//
//  ResetPasswordViewController.m
//  Comma
//
//  Created by dlios on 15-1-14.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ResetPasswordViewController.h"
#import "GlobalMethod.h"
#import "URLMacro.h"
#import "ResetPasswordView.h"
#import "Reachability.h"
#import "GlobalMethod.h"
#import "ConnectModel.h"
#import <CommonCrypto/CommonDigest.h>
#import "YRSideViewController.h"
@interface ResetPasswordViewController ()<UIAlertViewDelegate>

@end

@implementation ResetPasswordViewController

- (void)loadView
{
    [super loadView];
    // 不能划出侧边栏
    YRSideViewController *slide = [YRSideViewController share];
    slide.needSwipeShowMenu = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.customView = [[ResetPasswordView alloc] initWithUpTextField:@"重复新密码" DownTextField:@"验证码" UPButton:@"下一步" DownButton:@"取 消" LeftButton:nil RightButton:nil];
//    self.view = _customView;
//    [_customView addCustomUI];
//    [self.customView.upButton addTarget:self action:@selector(upButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.customView.downButton addTarget:self action:@selector(downButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.customView.authButton addTarget:self action:@selector(authButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)upButtonAction:(id)sender
{
    NSString * currentDeviceUUIDStr = [GlobalMethod GetIOSUUID];
    NSString *md5String = [GlobalMethod getApproveKey];
    NSString * netString = [NSString stringWithFormat:@"%@?mobile=%@&password=%@&confirmPassword=%@&usingKey=%@&deviceID=%@&code=%@", URL_USER_PASSWORDGET,@"15998295330", self.customView.passWordField.text, self.customView.upTextField.text, currentDeviceUUIDStr, md5String, self.customView.downTextField.text];
//    [AFNetWorkingYU netWorkWithURL:URL_USER_PASSWORDGET body:netString resultBlock:^(id result) {
//        NSLog(@"%@", result);
//    }];
    [ConnectModel connectModelWithUrl:netString parmaters:nil finish:^(NSData *data) {
        NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"==========%@", dic);
    }];
}

- (void)downButtonAction:(id)sender
{
    NSLog(@"dd");
}

- (void)authButtonAction:(id)sender
{
    if (![self isValidateMobile:self.customView.mobileField.text]) {
        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"注意" message:@"请输入正确的手机号" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
        alertView.delegate =self;
        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(closeAlert:) userInfo:alertView repeats:NO];
        [alertView show];
    }
    else {
        Reachability * reach = [Reachability reachabilityWithHostname:@"www.baidu.com"];
        NetworkStatus status = [reach currentReachabilityStatus];
        if (status!= NotReachable) {
            [self sendAuthCode];
            [self.customView.authButton setEnabled:NO];
        }
        else {
            [self makeAlertView];
        }
//        self.warningTimer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(makeAlertView) userInfo:nil repeats:NO];
//        [[NSRunLoop currentRunLoop] addTimer:_warningTimer forMode:NSDefaultRunLoopMode];
        
    }
}

/*手机号码验证 MODIFIED BY HELENSONG*/
-(BOOL) isValidateMobile:(NSString *)mobile
{
    //手机号以13， 15，18开头，八个 \d 数字字符
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    //    NSLog(@"phoneTest is %@",phoneTest);
    return [phoneTest evaluateWithObject:mobile];
}

- (void)sendAuthCode
{
    NSString * currentDeviceUUIDStr = [GlobalMethod GetIOSUUID];
    NSString *md5String = [GlobalMethod getApproveKey];
    NSString *str = [NSString stringWithFormat:@"%@?mobile=%@&deviceID=%@&usingKey=%@", URL_GET_AUTHCODE, @"15998295330",currentDeviceUUIDStr,md5String];
    [ConnectModel connectModelWithUrl:str parmaters:nil finish:^(NSData *data) {
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@", dic);
        if ([[dic objectForKey:@"msg"] isEqualToString:@"success"]) {
            [self resenderAction];
        }
        else
        {
            UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"注意" message:[dic objectForKey:@"msg"] delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil];
            [alertView show];
            [self.customView.authButton setEnabled:YES];
        }
    }];
    
}

- (void)closeAlert:(NSTimer*)timer {
    [(UIAlertView*) timer.userInfo  dismissWithClickedButtonIndex:0 animated:YES];
}

- (void)makeAlertView
{
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"注意" message:@"网络连接错误!" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil];
    [alertView show];
    [self.customView.authButton setEnabled:YES];
}

- (void)resenderAction
{
    
    self.resenderTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(changeAuthButtonAction:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_resenderTimer forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] run];
}

- (void)changeAuthButtonAction:(NSTimer *)timer
{
    static NSInteger timeRemain= 10;
    if (timeRemain == 0) {
        [self.customView.coverLabel setFrame:CGRectMake(SCREEN_WIDTH, 400,100, 44)];
        [self.customView.authButton setEnabled:YES];
        [_resenderTimer invalidate];
        _resenderTimer = nil;
        timeRemain = 10;
        return;
    }
    [self.customView.coverLabel setText:[NSString stringWithFormat:@"发送成功, %ld秒后重新发送 ", timeRemain]];
    //    [_authButton setBackgroundColor:[UIColor blackColor]];
    [self.customView.coverLabel sizeToFit];
    [self.customView.coverLabel setFrame:CGRectMake(SCREEN_WIDTH - self.customView.coverLabel.frame.size.width, 400, self.customView.coverLabel.frame.size.width, 44)];
    timeRemain--;
}



@end
